import router from './router';
import store from './store';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

NProgress.configure({ showSpinner: false });

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  // 防止刷新后vuex里丢失token
  store.commit('user/getToken');
  // 防止刷新后vuex里丢失标签列表tagList
  store.commit('tab/getMenu');
  let token = store.getters.token;
  // 过滤登录页，防止死循环
  if (!token && to.name !== 'login') {
    next({ name: 'login' });
    NProgress.done();
  } else {
    next();
  }
});

router.afterEach(() => {
  NProgress.done();
});
